package com.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.domain.Dept;
import com.li.domain.RoleDept;
import com.li.exception.NotFondException;
import com.li.mapper.DeptMapper;
import com.li.mapper.RoleDeptMapper;
import com.li.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private RoleDeptMapper roleDeptMapper;

    @Override
    public List<Dept> listQueryDept(String name) {
        return deptMapper.listQueryDept(name);
    }

    @Override
    public String addDept(Dept dept) {
        if (dept==null){
            throw new NotFondException("部门为空");
        }
        dept.setCreateTime(new Date());
        int insert = deptMapper.insert(dept);
        return String.valueOf(insert);
    }

    @Override
    public String updateDept(Dept dept) {
        if (dept==null){
            throw new NotFondException("部门为空");
        }
        int insert = deptMapper.updateById(dept);
        return String.valueOf(insert);
    }

    @Override
    public List<Dept> listQueryDeptTree(Dept dept) {
        return deptMapper.listQueryDeptTree(dept);
    }

    @Transactional
    @Override
    public boolean delDept(Integer deptId) {
        if (deptId==null){
            return false;
        }
        try {
            roleDeptMapper.delete(new QueryWrapper<RoleDept>().eq("dept_id",deptId));
            deptMapper.deleteById(deptId);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
